package com.xc.h05.controller.lishenao;

import com.xc.h05.entity.Article;
import com.xc.h05.entity.Consult;
import com.xc.h05.service.lishenao.ArticleService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class ArticleConrtroller {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/article")
    public R selectAll(Integer pageNum, Integer pageSize,Article article){
        return R.ok(articleService.findAll(pageNum, pageSize, article));
    }

    @PostMapping("/addarticle")
    public R add(@RequestBody Article article){
        int i = articleService.insertSelective(article);
        if (i>0){
            return R.ok("添加成功");
        }else   {
            return R.error();
        }
    }

    @PutMapping("/updatearticle")
    public R update(@RequestBody Article article){
        int i = articleService.updateByPrimaryKeySelective(article);
        if (i>0){
            return R.ok("修改成功");
        }else   {
            return R.error();
        }
    }

    @DeleteMapping("/deletearticle")
    public R delete(Long id){
        int i = articleService.deleteByPrimaryKey(id);
        if (i>0){
            return R.ok("删除成功");
        }else   {
            return R.error();
        }
    }

    @GetMapping("/affconsult")
    public R affconsult(){
        List<Consult> affConsult = articleService.findAffConsult();
        return R.ok(affConsult);
    }

    @GetMapping("/findbyid")
    public R findById(Long id){
        Article byId = articleService.findById(id);
        return R.ok(byId);
    }

}
